package tp.pr3;

import java.util.ArrayList;

import tp.pr3.instructions.*;
import tp.pr3.instructions.exceptions.WrongInstructionFormatException;

public class Interpreter {
	private static final String LINE_SEPARATOR = System
			.getProperty("line.separator");

	/**
	 * Genera una nueva instruccion de acuerdo a lo introducido por el usuario
	 * 
	 * @param line
	 *            - un string con el input del usuario
	 * @return la nueva instruccion
	 * 
	 */
	public static Instruction generateInstruction(String line)
			throws WrongInstructionFormatException {
		ArrayList<Instruction> instructions = new ArrayList<Instruction>();

		instructions.add(new QuitInstruction());
		instructions.add(new HelpInstruction());
		instructions.add(new MoveInstruction());
		instructions.add(new DropInstruction());
		instructions.add(new OperateInstruction());
		instructions.add(new PickInstruction());
		instructions.add(new RadarInstruction());
		instructions.add(new ScanInstruction());
		instructions.add(new TurnInstruction());

		for (int i = 0; i < instructions.size() - 1; ++i) {
			try {
				return instructions.get(i).parse(line);
			} catch (WrongInstructionFormatException e) {

			}
		}

		try {
			return instructions.get(instructions.size() - 1).parse(line);
		} catch (WrongInstructionFormatException e) {
			throw e;
		}

	}

	/**
	 * Devuelve informacion sobre las instrucciones entendidas por el robot
	 * 
	 * @return un string con todas las instrucciones disponibles
	 */
	public static String interpreterHelp() {
		String help = Message.VALID_INSTRUCTIONS + LINE_SEPARATOR;

		ArrayList<Instruction> instructions = new ArrayList<Instruction>();

		instructions.add(new QuitInstruction());
		instructions.add(new HelpInstruction());
		instructions.add(new MoveInstruction());
		instructions.add(new DropInstruction());
		instructions.add(new OperateInstruction());
		instructions.add(new PickInstruction());
		instructions.add(new RadarInstruction());
		instructions.add(new ScanInstruction());
		instructions.add(new TurnInstruction());

		for (Instruction i : instructions) {
			help += i.getHelp() + LINE_SEPARATOR;
		}

		return help;
	}
}
